<template>
    <view style="overflow-y: hidden;">
        <div v-if="isLoading" class="loading-overlay">
            <div class="loading-spinner">
                {{loadingText}}
            </div> 
        </div>

        <view class="content">
            <!---------------------------------- 扫描 ---------------------------------->
{{tableList}}
		</view>
    </view>

</template>

<script>
    import {
        log,
        getTagIndex
    } from "../../common/util.js";

    // 获取 module
    var rfidModule = uni.requireNativePlugin("DeviceModule_RFID");

    export default {
        components: {},
        data() {
            return {
                isLoading: true,
                loadingText: 'RFID模块加载中...',
                items: ['扫描', '读-写', '锁定', '销毁', '设置'],
                current: 0,
                activeColor: '#767676',
                styleType: 'button',

                //扫描页面
                scanCbFilter: false,
                scanFilterAddress: "32",
                scanFilterLength: "96",
                scanFilterData: "",
                scanFilterBank: "1",
                buttonText: "开始扫描",
                tagCount: 0,
                count: 0,
                tagList: [],
                pageSize: 10,
                pageCurrent: 1,
                tableList: [],

            }
        },
        onLoad: function() {
            let self = this
            // #ifdef APP-PLUS
            // 手柄按键监听事件，可在此编写一些逻辑，如使用按键触发扫描，
            plus.key.addEventListener('keydown', function(KeyEvent) {
                log("按下了键：" + KeyEvent.keyCode);
                if (KeyEvent.keyCode == 293) {
                    self.scanSwitch()
                }
            });
            // #endif   
        },
        mounted() {
            setTimeout(() => {
                // 使用模块前必须先初始化RDIF模块
                let res = rfidModule.init();
                log(JSON.stringify(res));
                if (res.code == 0) {
                    this.isLoading = false;
                } else if (res.code == -1) {
                    this.loadingText = 'RFID模块加载失败'
                    // uni.showToast({
                    //     title: 'RFID模块加载失败'
                    // })
                }
            }, 400);

        },
        beforeDestroy() {
            // 使用完毕必须释放RDIF模块
            let res = rfidModule.free();
            log("free: " + JSON.stringify(res))
        },

        methods: {
            // onClickItem(e) {
            //     if (this.current !== e.currentIndex) {
            //         this.current = e.currentIndex
            //     }
            //     if (this.current === 4) {
            //         this.getFreModeClick(false)
            //         this.getPowerClick(false)
            //         this.getProtocolClick(false)
            //         this.getRFLinkClick(false)
            //         this.getSessionClick(false)
            //         this.getScanModeClick(false)
            //     }
            // },
            /*******************************************扫描******************************************/
            scanSwitch() {
                log('scanSwitch')
                if (this.buttonText === "开始扫描") {
                    this.startScan()
                    this.buttonText = "停止扫描"
                } else {
                    this.stopScan()
                    this.buttonText = "开始扫描"
                }
            },
            async startScan() {
                log('rfidModule = ' + rfidModule)
                // 调用异步方法
                rfidModule.startScan(
                    (res) => {
                        log(JSON.stringify(res))
                        if (res.code == 0) {
                            uni.showToast({
                                title: '开启扫描成功',
                            })
                        } else if (res.code == 1) {
                            // this.lists.unshift(JSON.stringify(data))
                            if (res.data != null && res.data.length > 0) {
                                for (let i = 0; i < res.data.length; i++) {
                                    let tag = res.data[i];
                                    let tagExists = [];
                                    //过滤重复标签
                                    let index = getTagIndex(this.tagList, tag.epc, tagExists)
                                    if (tagExists[0]) { //标签已存在
                                        this.tagList[index].rssi = tag.rssi;
                                        this.tagList[index].ant = tag.antennaPort;
                                        this.tagList[index].count++;
                                    } else { //标签不存在
                                        this.tagList.splice(index, 0, {
                                            epc: tag.epc,
                                            tid: tag.tid || '',
                                            user: tag.user || '',
                                            rssi: tag.rssi,
                                            pc: tag.pc,
                                            count: 1
                                        });
                                        this.tagCount = this.tagList.length
                                    }
                                    this.count++;
                                }

                                this.pageCurrent = 1
                                this.tableList = this.tagList.slice(
                                    (this.pageCurrent - 1) * this.pageSize,
                                    this.pageCurrent * this.pageSize
                                );
                            }
                        }
                    }
                );
            },
            stopScan() {
                // 调用同步方法
                var res = rfidModule.stopScan()
                log(JSON.stringify(res));
                if (res.code == 0) {
                    uni.showToast({
                        title: '关闭扫描成功',
                    })
                } else {
                    uni.showToast({
                        icon: "error",
                        title: res.message,
                    })
                }
            },
            setFilter() {
                let res = rfidModule.setFilter({
                    "bank": this.scanFilterBank,
                    "ptr": this.scanFilterAddress,
                    "cnt": this.scanFilterLength,
                    "data": this.scanFilterData,
                });
                log(JSON.stringify(res))
                if (res.code == '0') {
                    uni.showToast({
                        title: '成功',
                    })
                } else {
                    uni.showToast({
                        icon: "error",
                        title: res.message,
                    })
                }
            },
            clearData() {
                this.tagList = []
                this.count = 0
                this.tagCount = 0
                this.tableList = []
                this.pageCurrent = 1
            },
            scanPagechange(e) {
                this.pageCurrent = e.current
                this.tableList = this.tagList.slice(
                    (this.pageCurrent - 1) * this.pageSize,
                    this.pageCurrent * this.pageSize
                );
            },
            copy(value) {
                log(value)
                uni.setClipboardData({
                    data: String(value),
                    success: () => {
                        log(value)
                    }
                })
            },

            onScanAddressInput: function(event) {
                this.scanFilterAddress = event.target.value
            },
            onScanLengthInput: function(event) {
                this.scanFilterLength = event.target.value
            },
            onScanDataInput: function(event) {
                this.scanFilterData = event.target.value
            },


        }
    }
</script>

<style >


    .loading-overlay {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0, 0, 0, 0.5);
        z-index: 9999;
        display: flex;
        justify-content: center;
        align-items: center;
    }


</style>